package com.morningstar.qs.api.basic.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class UnicodeUtil {
	
	
	public static void main(String args[]){
		String s = "\u0061\u006d";
		System.out.print(decodeUnicode(s));
	}
	
	
	public static String encodeUnicode(final String gbString) {
		char[] utfBytes = gbString.toCharArray();
		String unicodeBytes = "";
		for (int byteIndex = 0; byteIndex < utfBytes.length; byteIndex++) {
			String hexB = Integer.toHexString(utfBytes[byteIndex]);
			if (hexB.length() <= 2) {
				hexB = "00" + hexB;
			}
			unicodeBytes = unicodeBytes + "\\u" + hexB;
		}
		return unicodeBytes;
	}
	
	
	public static  String decodeUnicode(String str)	{
		if(str==null || str.length()==0) return str;
		Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");   
        Matcher matcher = pattern.matcher(str);
        char ch;
        while (matcher.find()) {
            ch = (char) Integer.parseInt(matcher.group(2), 16);
            str = str.replace(matcher.group(1), ch + "");   
        }
        return str;
	}	
}
